package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品分类
 *
 * @Author mr.lcc
 * @Data 2025/5/20 20:29
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "菜品分类相关接口") // 添加类级别的Swagger注解
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增分类
     */
    @PostMapping
    @ApiOperation(value = "新增菜品分类") // 添加方法级别的Swagger注解
    public Result save(@RequestBody CategoryDTO category) {
        log.info("新增分类:{}", category);
        if (category == null) {
            throw new BaseException("请求参数为空");
        }
        Category newCategory = new Category();
        BeanUtils.copyProperties(category, newCategory);
        categoryService.save(newCategory);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询菜品分类") // 添加方法级别的Swagger注解
    public Result<PageResult> page(CategoryPageQueryDTO dto) {
        log.info("分页查询:{}", dto);
        PageResult pageResult = categoryService.pageList(dto);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除菜品分类") // 添加方法级别的Swagger注解
    public Result deleteById(Integer id) {
        log.info("删除分类:{}", id);
        if (id == null) {
            throw new BaseException("删除参数为空");
        }
        Category byId = categoryService.getById(id);
        if (byId == null) {
            throw new BaseException("分类不存在");
        }
        categoryService.removeById(id);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品分类") // 添加方法级别的Swagger注解
    public Result update(@RequestBody CategoryDTO category) {
        log.info("修改分类:{}", category);
        if (category == null) {
            throw new BaseException("请求参数为空");
        }
        Category newCategory = new Category();
        BeanUtils.copyProperties(category, newCategory);
        categoryService.updateById(newCategory);
        return Result.success();
    }

    /**
     * 查询分类
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有分类")
    public Result<List<Category>> list(Long type) {
        log.info("查询所有分类");
        List<Category> list = categoryService.list(new QueryWrapper<Category>().eq("type", type));
        return Result.success(list);
    }
}
